// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System.Linq.Expressions;
using Microsoft.Extensions.DependencyInjection;
using static Common.Grammar.Antlr.CommonParser;

namespace SnapObjects.Expressions
{
    public static partial class ExpressionsServiceCollectionExtensions
    {
        public static IServiceCollection AddCompileExpression(this IServiceCollection services)
        {
            services
                .AddExpressionCore<Expression>()
                .AddCompileVisitors()
                .AddCompileMetadataProviders();

            services.AddTransient<CompileMethodMatcher>();

            services.AddTransient<ICompileExpressionParser, CompileExpressionParser>();

            services.AddTransient<IPolicyProvider<Expression>, CompilePolicyProvider>();

            services.AddTransient<IMetadataManager<Expression>, CompileMetadataManager>();

            services.AddTransient<INestParseProvider<Expression>, CompileNestParseProvider>();

            return services;
        }

        private static IServiceCollection AddCompileVisitors(this IServiceCollection services)
        {
            services.AddVisitors<Expression>();

            services.AddTransient<INodeExpressionVisitor<Expression>, CompileNewExpressionVisitor<Expression_newContext>>();

            return services;
        }

        private static void AddCompileMetadataProviders(this IServiceCollection services)
        {
            services.AddTransient<IMetadataProvider<Expression>, DefaultCompilePropertyProvider>();
        }
    }
}
